<?php

namespace app\api\controller;

use app\api\controller\BaseController;
use app\api\model\Constructors as ConstructorsModel;
use app\api\model\WechatLogin as WechatLoginModel;
use app\api\Util\SMSHelper;
use app\api\Util\CCPHelper;
use Firebase\JWT\JWT;
use think\Db;


class User extends BaseController
{   
    /**
     * 用户登录
     * @param  string $mobile   [description]
     * @param  string $vCode    [description]
     * @return [type]           [description]
     */
    public function login($username = '', $pwd = '')
    {
        $ConstructorsModel = new ConstructorsModel;
        $userLogic = \think\Loader::model('UserLogic','logic');

        $data = [
            'username' => $username,
            'password' => $pwd
        ];
        $result = $this->validate($data, 'UserLogin');
        if (true !== $result) {
            // 验证失败 输出错误信息
            return base_result(null, false, 20001, $result);
        }
        // 验证用户密码是否存在
        $user = $ConstructorsModel->get_user_info($username, $pwd);
        // dump($user);die;
        if ($user) {
            $secretKey = 'bomeiyi';
            // 定义载荷
            $currentTime = time();
            $expTime = $currentTime + 30 * 24 * 3600;
            $payload = array(
                'iat' => $currentTime, // 有效期
                'exp' => $expTime,
                'data' => array(
                    'user_id' => $user['id'],
                )
            );
            $privateToken = JWT::encode($payload, $secretKey);
            header('token:' . $privateToken);
            $returnData = [
                'user_info' => $user,
            ];
            return base_result($returnData);
        } else {
            return base_result(null, false, 10004, '密码错误');
        }
    }

    /**
     * 获取用户容联云账号
     * @return [type] [description]
     */
    public function get_ronglian_account()
    {
        $userInfo = getTokenInfo();
        if (!$userInfo) 
            return base_result(null, false, '10001', '请登录');
        $user_id = $userInfo->user_id;
        // $user_id = 1;

        $ConstructorsModel = new ConstructorsModel;
        $userLogic = \think\Loader::model('UserLogic','logic');

        $username = $ConstructorsModel->where('id', $user_id)->value('username');
        $ronglianAccount = $userLogic->user_sub_account($user_id, $username);
        return base_result(array('ronglian_info' => $ronglianAccount));
    }

    /**
     * 发送手机验证码
     * @param  string $mobile [description]
     * @return [type]         [description]
     */
    public function send_mobile_vcode($mobile = '')
    {
        $sms_helper = new SMSHelper;
        $vCodeModel = \think\Loader::model('VerificationCode');
        $vCodeLogic = \think\Loader::model('VerificationCode','logic');

        $is_mobile = is_mobile($mobile);
        if (true !== $is_mobile) {
            // 验证失败 输出错误信息
            return base_result(null, false, 20004, '手机号码格式不正确');
        }

        $check_mobile = $vCodeLogic->check_mobile_code($mobile);
        if (true !== $check_mobile) {
            return base_result(null, false, 20005, '频繁操作');
        }

        $code = generate_code();
        $result= $sms_helper->send_msg($mobile, $code);
        if (!isset($result->result->success)) {
            return base_result(null, false, 20003, $result->msg);
        } else {
            $vCodeData = ['mobile' => $mobile, 'vcode' => $code, 'add_time' => date('Y-m-d H:i:s', time()+30*60)];
            $result = $vCodeModel->insert($vCodeData);
            return base_result(true);
        }
    }



    

    
}